//
//  SearchViewController.m
//  EasyToTesco2.0
//
//  Created by 袖手旁觀 on 16/6/7.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "SearchViewController.h"


#define ScreenWidth [UIScreen mainScreen].bounds.size.width

@interface SearchViewController ()<UISearchBarDelegate>

@property (nonatomic,strong)UISearchBar *searchBar;

@end

@implementation SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self addSearchTextField];
    self.tableView.tableFooterView = [[UIView alloc] init];
}

- (void)addSearchTextField {
    
    CGRect frame;
    if (WIDTH == 320) {
        frame = CGRectMake(-25, 0, 200, 30);
    } else {
        frame = CGRectMake(0, 0, 200, 30);
    }
    
    
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth * 0.7, 30)];
    view.backgroundColor = [UIColor whiteColor];
    view.layer.masksToBounds = YES;
    view.layer.cornerRadius = 6;
    view.layer.borderColor = [UIColor colorWithRed:100 / 255.0 green:100 / 255.0 blue:100 / 255.0 alpha:1].CGColor;
    view.layer.borderWidth = 0.2;
    
    
    self.searchBar = [[UISearchBar alloc] initWithFrame:frame];
    _searchBar.placeholder = @"在此输入商品名称";
    _searchBar.barTintColor = [UIColor whiteColor];
    _searchBar.keyboardType = UIKeyboardAppearanceDefault;
    _searchBar.delegate = self;
    
    self.navigationItem.titleView = _searchBar;
}


- (void)searchBut {
    NSLog(@"%s",__func__);
}


- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    NSLog(@"%s",__func__);
    [self.searchBar endEditing:NO];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return [self addSectionHeader:@"sousuo_1" andRightImageViewString:@"sousuo_0" andTitle:@"历史搜索"];
    }
    return [self addSectionHeader:@"sousuo_2" andRightImageViewString:nil andTitle:@"热门搜索"];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        
        static NSString *ID = @"sectionOne";
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
            
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 30)];
        label.text = @"暂无历史搜索";
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor blackColor];
        label.font = [UIFont systemFontOfSize:14];
        
        [cell.contentView addSubview:label];
        
        return cell;
    } else {
        static NSString *Id = @"sectionTwo";
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Id];
        
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Id];
        
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        cell.textLabel.text = @"text";
        
        return cell;
    }
}


- (UIView *)addSectionHeader:(NSString *)leftImageViewString andRightImageViewString:(NSString *)rightImageViewString andTitle:(NSString *)sectionHeaderTitle {
    
    UIView *sectionHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 40)];
    
    UIImageView *leftImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:leftImageViewString]];
    [leftImageView setFrame:CGRectMake(10, 10, 20, 20)];
    [sectionHeaderView addSubview:leftImageView];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(35, 5, 60, 30)];
    label.text = sectionHeaderTitle;
    label.textColor = [UIColor lightGrayColor];
    label.font = [UIFont systemFontOfSize:12];
    [sectionHeaderView addSubview:label];
    
    if (rightImageViewString.length != 0) {
        
        UIImageView *rightImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:rightImageViewString]];
        CGFloat x = self.view.width - 5 - 30;
        [rightImageView setFrame:CGRectMake(x, 10, 20, 20)];
        [sectionHeaderView addSubview:rightImageView];
    }
    
    return sectionHeaderView;
}

@end
